<!doctype html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <title>Synchronized live playback</title>

    <script src="../../dist/modern/umd/dash.all.debug.js"></script>

    <!-- Bootstrap core CSS -->
    <link href="../lib/bootstrap/bootstrap.min.css" rel="stylesheet">
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.4.1/font/bootstrap-icons.css">
    <link href="../lib/main.css" rel="stylesheet">

    <script class="code">

        function init() {
            var player1, player2, video;
            var MPD_2S_SEGMENTS = 'https://livesim2.dashif.org/livesim2/testpic_2s/Manifest.mpd';
            var settings = {
                streaming: {
                    delay: {
                        liveDelay: 10
                    },
                    liveCatchup: {
                        enabled: true
                    }
                }
            }

            video = document.querySelector('#video1');
            player1 = dashjs.MediaPlayer().create();
            player1.initialize(video, MPD_2S_SEGMENTS, true);
            player1.updateSettings(settings);

            video = document.querySelector('#video2');
            player2 = dashjs.MediaPlayer().create();
            player2.initialize(video, MPD_2S_SEGMENTS, true);
            player2.updateSettings(settings);


            setInterval(function () {
                var d = new Date();
                var seconds = d.getSeconds();
                document.querySelector('#sec').innerHTML = (seconds < 10 ? '0' : '') + seconds;
                var minutes = d.getMinutes();
                document.querySelector('#min').innerHTML = (minutes < 10 ? '0' : '') + minutes + ':';
                for (var i = 1; i < 3; i++) {
                    var p = eval('player' + i);
                    document.querySelector('#video' + i + 'delay').innerHTML = Math.round((d.getTime() / 1000) - Number(p.timeAsUTC()));
                    document.querySelector('#video' + i + 'buffer').innerHTML = p.getBufferLength() + 's';
                }


            }, 1000);

        }
    </script>
    <style>

        table {
            border-spacing: 10px;
        }

        video {
            width: 320px;
            height: 180px;
        }

        .clock {
            color: #000;
            font-size: 60pt
        }
    </style>
</head>
<body>

<main>
    <div class="container py-4">
        <header class="pb-3 mb-4 border-bottom">
            <img class=""
                 src="../lib/img/dashjs-logo.png"
                 width="200">
        </header>
        <div class="row">
            <div class="col-md-8">
                <div class="h-100 p-5 bg-light border rounded-3">
                    <h3>Synchronized live playback</h3>
                    <p>This sample illustrates how to use the catchup mechanism to synchronize media playback of two
                        videos.</p>
                </div>
            </div>
            <div class="col-md-4">
                <div class="h-100 p-5 border rounded-3">
                    <h3>Wall Clock reference time</h3>
                    <div class="clock">
                        <span id="min"> </span><span id="sec"></span>
                    </div>
                </div>
            </div>
        </div>
        <div class="row margin-top-row">
            <div class="col-md-6">
                <div class="h-100 p-5 border rounded-3">
                    <h5> 2s segment, 10s target latency</h5>
                    <div class="code">
                        <video id="video1" controls="true" muted></video>
                        <div>
                            <div><i class="bi bi-arrow-right-square"></i> Seconds behind live: <span
                                id="video1delay"></span></div>
                            <div><i class="bi bi-arrow-right-square"></i> Buffer length: <span
                                id="video1buffer"></span></div>
                        </div>
                    </div>
                </div>
            </div>
            <div class="col-md-6">
                <div class="h-100 p-5 border rounded-3">
                    <h5> 2s segment, 10s target latency</h5>
                    <div class="code">
                        <video id="video2" controls="true" muted></video>
                        <div>
                            <div><i class="bi bi-arrow-right-square"></i> Seconds behind live: <span
                                id="video2delay"></span></div>
                            <div><i class="bi bi-arrow-right-square"></i> Buffer length: <span
                                id="video2buffer"></span></div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
        <div class="row">
            <div class="col-md-12">
                <div id="code-output"></div>
            </div>
        </div>
        <footer class="pt-3 mt-4 text-muted border-top">
            &copy; DASH-IF
        </footer>
    </div>
</main>
<script class="code">
    document.addEventListener('DOMContentLoaded', function () {
        init();
    });
</script>
<script src="../highlighter.js"></script>
</body>
</html>
